/*
	Prepare Baseline Data for appending
*/

********************************************************************************
*  LOAD DATA AND KEEP ESSENTIAL VARIABLES
********************************************************************************

use "$data/1_raw/baseline_raw-deidentified.dta", clear

keep ///
    matricule sexe treatment ///
    s1qs1 ///
    birth_year s1q4 s1q5a s1q5a_a ///
    s1q14 s1q14c ///
    s1q15a s1q15 ///
    s2q5 s2q5a ///
    s3q1 s3q2 s3q3 s3q4 s3q5 s3q6 s3q7 s3q8 s3q9 s3q10 ///
    s4q1 s4q2 s4q3 s4q4 s4q5 s4q6 s4q7 s4q8 s4q9 s4q10 s4q11 ///
    s4q12 s4q13 s4q14 s4q15 s4q16 s4q17 s4q18 s4q19 s4q20 s4q21 ///
    s4q22 s4q23 s4q24 s4q25 s4q26 s4q27 s4q28 s4q29 s4q30 s4q31 s4q32 ///
    s5q1 s5q2 s5q3 s5q5 s5q6 s5q7 s5q8 s5q9

********************************************************************************
*  VARIABLE RENAMING
********************************************************************************

g survey_round = 1

// Core identification
rename matricule id
rename sexe sex
rename treatment treat

// Survey metadata
rename s1qs1 consent

// Demographics
rename birth_year yob
rename s1q4 educ

// Employment
rename s1q5a eif_pos

// Work hours
rename s1q14 last7d_hours_worked

// Earnings
rename s1q14c earn_avg_month
rename s1q15a earn_avg_month_range
rename s1q15 earn_last_month
rename s2q5 last7d_oj_earn
rename s2q5a last7d_oj_earn_range

// Big 5 personality
rename s3q1 big5_extrovert
rename s3q2 big5_critic
rename s3q3 big5_reliable
rename s3q4 big5_anxious
rename s3q5 big5_open
rename s3q6 big5_shy
rename s3q7 big5_sympathetic
rename s3q8 big5_disorganized
rename s3q9 big5_calm
rename s3q10 big5_conventional

// Conscientiousness traits
rename s4q1  trait_remorse_late
rename s4q2  trait_make_plan
rename s4q3  trait_work_goals
rename s4q4  trait_tidy
rename s4q5  trait_earlier_meet
rename s4q6  trait_avoid_late
rename s4q7  trait_follow_schedule
rename s4q8  trait_followup_method
rename s4q9  trait_attentive_others
rename s4q10 trait_others_dependent
rename s4q11 trait_set_clear_goals
rename s4q12 trait_keep_promises
rename s4q13 trait_avoid_procrast
rename s4q14 trait_fulfill_promises
rename s4q15 trait_set_high_standards
rename s4q16 trait_organize_schedule
rename s4q17 trait_change_setback
rename s4q18 trait_up_to_responsib
rename s4q19 trait_on_time
rename s4q20 trait_respect_engagements
rename s4q21 trait_work_hard
rename s4q22 trait_clean_up
rename s4q23 trait_orga_pers_spaces
rename s4q24 trait_others_count
rename s4q25 trait_focus_goals
rename s4q26 trait_make_plan_for_goal
rename s4q27 trait_late_meetings
rename s4q28 trait_keep_organized
rename s4q29 trait_keep_tidy
rename s4q30 trait_fulfill_duty
rename s4q31 trait_keep_trying_if_fail
rename s4q32 trait_manage_responsib

// Household characteristics
rename s5q1 hh_n_above_18
rename s5q2 hh_n_children
rename s5q3 hh_n_bedrooms
rename s5q5 hh_revenue_range_r1r2
rename s5q6 hh_formal_savings
rename s5q7 hh_informal_savings
rename s5q8 hh_formal_debt
rename s5q9 hh_informal_debt

********************************************************************************
*  PROCESS ESSENTIAL VARIABLES
********************************************************************************

* -------------------- ID -------------------- *
destring id, replace

* -------------------- Demographics -------------------- *
* Sex is already coded 0/1
label def sex 0 "Male" 1 "Female"
label val sex sex

* Treatment is already coded 0/1
label def treat 0 "Control" 1 "Treatment", replace
label val treat treat

* Process education
label def educ ///
	0 "None" 1 "Primary, incomplete" 2 "Primary, complete" ///
	3 "Middle, incomplete" 4 "Middle, complete" ///
	5 "Secondary, incomplete" 6 "Secondary, complete" ///
	7 "Higher education"
label val educ educ

* -------------------- Consent -------------------- *
recode		consent (2=0)

label def	yesno 0 "No" 1 "Yes"
label val	consent yesno

keep if consent == 1

* -------------------- Work variables -------------------- *

mvdecode last7d_*, mv(-888 -999)
mvdecode last7d_oj_earn, mv(999)
mvdecode earn_*, mv(-888 -999)
mvdecode earn_avg_month earn_last_month, mv(-888 -999 .999 999 888)
replace earn_avg_month = . if earn_avg_month>0 & earn_avg_month<1
replace earn_last_month = . if earn_last_month>0 & earn_last_month<1

* -------------------- Household variables -------------------- *

mvdecode hh_n_above_18 hh_n_children hh_n_bedrooms, mv(-888 -999)

mvdecode hh_formal_savings hh_informal_savings hh_formal_debt hh_informal_debt, mv(-888 -999)

label def savings_debt ///
	0 "Nothing (0 CFAF)" 1 "Less than 50,000" 2 "50,001–100,000" ///
	3 "100,001–150,000" 4 "150,001–200,000" 5 "200,001–250,000" ///
	6 "More than 250,000"

label val hh_formal_savings hh_informal_savings hh_formal_debt hh_informal_debt savings_debt

mvdecode 	hh_revenue_range_r1r2, mv(-888 -999) // turning into missing values 

label def 	hh_revenue_range_r1r2					///
    1 "Less than 80,000" ///
    2 "80,001–130,000" ///
    3 "130,001–180,000" ///
    4 "180,001–230,000" ///
    5 "230,001–280,000" ///
    6 "More than 280 000 (CFAF)"

lab val		hh_revenue_range_r1r2 hh_revenue_range_r1r2

mvdecode hh_*, mv(-888 -999)

* -------------------- Position at Eiffage -------------------- *

replace 	eif_pos=13		if 		s1q5a_a=="Manoeuvre specialise"|s1q5a_a=="Manoeuvre spetialise"|s1q5a_a=="Ouvrier specialise"
replace 	eif_pos=9		if 		s1q5a_a=="Aide conducteur"
replace 	eif_pos=10		if 		s1q5a_a=="Aide topographe"|s1q5a_a=="Topographe"
replace 	eif_pos=11		if 		s1q5a_a=="Ouvrier"
replace 	eif_pos=12		if 		s1q5a_a=="Porte drapeau"

drop s1q5a_a

label def	eif_pos ///
    1 "Security agent helper"	2 "Guardian"	///
    3 "Mason"					4 "Scrap dealer"	///
    5 "Form setter"				6 "Carpenter"	///
    7 "Laborer"					8 "Other"	///
    9 "Driver help"				10 "Topographer/Topographer helper"	///
    11 "Worker"					12 "Flag holder"	///
    13 "Specialized laborer/worker"  ///
    14 "Preventive agent"		15 "Bile"	///
    16 "Painter"				17 "Waterproofing" ///
    18 "Restauration"			19 "QHSE supervisor/manager"  ///
    20 "Plaster worker"			21 "Welder"	///
    21 "Crane helper" 			22 "Sanitation" ///
    23 "Cleaner team leader" 	///
    24 "Fosseur"				25 "Workhouse worker" ///
    26 "Multi-pole Network Worker" 	27 "Earthwork worker" 

label val	eif_pos eif_pos

* ----------------- Earnings ----------------- *

label def 	monthly_income_range ///
    1 "Less than 80,000 (CFAF)" ///
    2 "80,000–130,000" ///
    3 "130,000–180,000" ///
    4 "180,000–230,000" ///
    5 "230,000–280,000" ///
    6 "More than 280,000" ///

label value earn_avg_month_range 	monthly_income_range



label value last7d_oj_earn_range monthly_income_range

* -------------------- Big Five variables -------------------- *
* Code missing values
mvdecode big5_*, mv(-888 -999)

* Reverse code where necessary (based on standard Big Five scoring)
recode big5_critic big5_anxious big5_shy big5_disorganized big5_conventional ///
    (1=5) (2=4) (3=3) (4=2) (5=1)

* -------------------- Conscientiousness traits -------------------- *
* Code missing values  
mvdecode trait_*, mv(-888 -999)

* Inverse the coding of answers to have "higher values = better outcomes"
recode		trait_* (1=5) (2=4) (3=3) (4=2) (5=1)

lab def		easiness ///
    5 "Very easy" ///
    4 "Fairly easy" ///
    3 "Neither easy nor difficult" ///
    2 "Fairly difficult" ///
    1 "Very difficult"

lab val		trait_* easiness

********************************************************************************
*  ORDER VARIABLES AND SAVE
********************************************************************************

* Save harmonized baseline data
save "$data/2_processed/baseline_clean.dta", replace

********************************************************************************
*  END CODE
********************************************************************************
